package pms.theme.action;

import java.util.ArrayList;
import java.util.List;

import pms.common.service.MasterService;
import pms.common.service.UserAuthService;
import pms.common.utils.DateUtil;
import pms.common.utils.PMSLog;
import pms.common.utils.SessionUtil;
import pms.common.value.MasterValue;
import pms.common.value.ProgressValue;
import pms.common.value.ProjectValue;
import pms.common.value.ReviewValue;
import pms.common.value.TargetValue;
import pms.common.value.ThemeValue;
import pms.common.value.UserSessionValue;
import pms.theme.service.ThemeService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Maniac
 *
 */
public class ThemeListAction extends ActionSupport {
	
	private ThemeService themeService;
	private MasterService masterService;
	private UserAuthService userAuthService;
	private int reviewId;
	
	
	public int getReviewId() {
		return reviewId;
	}

	public void setReviewId(int reviewId) {
		this.reviewId = reviewId;
	}

	private ProgressValue addProgressValue = new ProgressValue();
	private ReviewValue addReviewValue = new ReviewValue();
	
	public ReviewValue getAddReviewValue() {
		return addReviewValue;
	}

	public void setAddReviewValue(ReviewValue addReviewValue) {
		this.addReviewValue = addReviewValue;
	}

	public ProgressValue getAddProgressValue() {
		return addProgressValue;
	}

	public void setAddProgressValue(ProgressValue addProgressValue) {
		this.addProgressValue = addProgressValue;
	}

	private List<MasterValue> targetTypeList;

	public List<MasterValue> getTargetTypeList() {
		return targetTypeList;
	}

	public void setTargetTypeList(List<MasterValue> targetTypeList) {
		this.targetTypeList = targetTypeList;
	}

	public UserAuthService getUserAuthService() {
		return userAuthService;
	}

	public void setUserAuthService(UserAuthService userAuthService) {
		this.userAuthService = userAuthService;
	}

	private List<ProjectValue> projectList;
	private List<ThemeValue> themeList;
	private List<UserSessionValue> userList;
	private List<MasterValue> targetStatusList;
	private List<MasterValue> reviewTypeList;
	
	
	
	public List<MasterValue> getReviewTypeList() {
		return reviewTypeList;
	}

	public void setReviewTypeList(List<MasterValue> reviewTypeList) {
		this.reviewTypeList = reviewTypeList;
	}

	public List<MasterValue> getTargetStatusList() {
		return targetStatusList;
	}

	public void setTargetStatusList(List<MasterValue> targetStatusList) {
		this.targetStatusList = targetStatusList;
	}

	public List<UserSessionValue> getUserList() {
		return userList;
	}

	public void setUserList(List<UserSessionValue> userList) {
		this.userList = userList;
	}

	private List<TargetValue> targetList;
	
	private TargetValue targetAddValue = new TargetValue();
	
	public List<TargetValue> getTargetList() {
		return targetList;
	}

	public void setTargetList(List<TargetValue> targetList) {
		this.targetList = targetList;
	}

	public TargetValue getTargetAddValue() {
		return targetAddValue;
	}

	public void setTargetAddValue(TargetValue targetAddValue) {
		this.targetAddValue = targetAddValue;
	}

	private ThemeValue addValue = new ThemeValue();
	public ThemeValue getAddValue() {
		return addValue;
	}

	public void setAddValue(ThemeValue addValue) {
		this.addValue = addValue;
	}

	private List<MasterValue> themeStatusList = new ArrayList<MasterValue>();
	
	public List<MasterValue> getThemeStatusList() {
		return themeStatusList;
	}

	public void setThemeStatusList(List<MasterValue> themeStatusList) {
		this.themeStatusList = themeStatusList;
	}

	private String buttonId = "";
	private int themeId;
	private int projectId;
	private int targetId;
	
	public int getTargetId() {
		return targetId;
	}

	public void setTargetId(int targetId) {
		this.targetId = targetId;
	}

	private int addMode=0;
	public int getAddMode() {
		return addMode;
	}

	public void setAddMode(int addMode) {
		this.addMode = addMode;
	}

	public MasterService getMasterService() {
		return masterService;
	}

	public void setMasterService(MasterService masterService) {
		this.masterService = masterService;
	}

	public int getProjectId() {
		return projectId;
	}

	public void setProjectId(int projectId) {
		this.projectId = projectId;
	}

	public int getThemeId() {
		return themeId;
	}

	public void setThemeId(int themeId) {
		this.themeId = themeId;
	}

	public String getButtonId() {
		return buttonId;
	}

	public void setButtonId(String buttonId) {
		this.buttonId = buttonId;
	}

	public List<ThemeValue> getThemeList() {
		return themeList;
	}

	public void setThemeList(List<ThemeValue> themeList) {
		this.themeList = themeList;
	}

	public List<ProjectValue> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<ProjectValue> projectList) {
		this.projectList = projectList;
	}

	public ThemeService getThemeService() {
		return themeService;
	}

	public void setThemeService(ThemeService themeService) {
		this.themeService = themeService;
	}

	private static final long serialVersionUID = 2276244650030984325L;
	
	private void prepareThemeView() {
		projectList = themeService.getProjectList();	
		themeList = themeService.getThemeList(projectId);
		themeStatusList = masterService.getMaster("theme_status");
	}
	private void prepareTargetView() {

		this.userList = this.userAuthService.getUserList();
		this.targetTypeList = this.masterService.getMaster("target_type");
		this.targetStatusList = this.masterService.getMaster("target_status");
		for(ThemeValue l_tv : this.getThemeList()) {
			if(l_tv.getThemeId()==this.getThemeId()) {
				l_tv.setMode(1);
				this.targetList = themeService.getTargetList(themeId);				
				break;
			}
		}
	}
	private void prepareProgressView() {
//		addProgressValue.setPgsUserId(SessionUtil.getUserId());
//		addProgressValue.setPgsTs(DateUtil.getCurrentDate());
		for(TargetValue tv : this.targetList) {
			if(tv.getTargetId() == this.targetId) {
				tv.setProgressList(themeService.getProgressList(this.targetId));
				tv.setMode(2);
			}
		}
	}
	
	private void prepareReviewView() {
		this.reviewTypeList = this.masterService.getMaster("review_type");
		for(TargetValue tv : this.targetList) {
			if(tv.getTargetId() == this.targetId) {
				tv.setReviewList(themeService.getReviewList(this.targetId, tv.getTargetName(), this.userList));
				tv.setMode(3);
			}
		}
	}
	
	@Override
	public String execute() {
		PMSLog.start();
		
		projectList = themeService.getProjectList();
		this.projectId = SessionUtil.getUserValue().getUserProjectId();
		System.out.println("UserprojectId=" + this.projectId);
		
			
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doList() {
		PMSLog.start();
		
		this.prepareThemeView();
		
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doUpdate() {
		PMSLog.start();
		for(ThemeValue l_tv : this.getThemeList()) {
			if(l_tv != null) {
				if(l_tv.getThemeId()==this.getThemeId()) {
					l_tv.setUpdUserId(SessionUtil.getUserId());
					themeService.updateTheme(l_tv);
					break;
				}
			}
		}	
		this.prepareThemeView();
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doEdit() {
		PMSLog.start();
		this.prepareThemeView();
		for(ThemeValue l_tv : this.getThemeList()) {
			if(l_tv.getThemeId()==this.getThemeId()) {
				l_tv.setEditFlag(1);
				break;
			}
		}
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doAdd() {
		PMSLog.start();
		this.prepareThemeView();
		this.setAddMode(1);
		this.getAddValue().setProjectId(projectId);
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doInsert() {
		PMSLog.start();
		addValue.setAddUserId(SessionUtil.getUserId());
		themeService.insertTheme(addValue);
		this.prepareThemeView();
		PMSLog.end();
		return SUCCESS;
	}
	public String doDelete() {
		PMSLog.start();
		
		themeService.deleteTheme(themeId, SessionUtil.getUserId());
		this.prepareThemeView();
		
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetList() {
		PMSLog.start();
		this.prepareThemeView();
		this.prepareTargetView();
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetListClose() {
		PMSLog.start();
		this.prepareThemeView();
		
		PMSLog.end();
		return SUCCESS;
	}
	public String doTargetEdit() {
		PMSLog.start();
		System.out.println("themeId:" + themeId);
		System.out.println("targetId:" + targetId);
		this.prepareThemeView();
		this.prepareTargetView();
		for(TargetValue tv : this.targetList) {
			if(tv.getTargetId()==this.targetId) {
				tv.setMode(1);
			}
		}
		PMSLog.end();
		return SUCCESS;
	}
	public String doTargetUpdate() {
		PMSLog.start();
		
		for(TargetValue tv : this.targetList) {
			if(tv.getTargetId()==this.targetId) {
				themeService.updateTarget(tv);
			}
		}
		this.prepareThemeView();
		this.prepareTargetView();		
		
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetAdd() {
		PMSLog.start();
		this.prepareThemeView();
		this.prepareTargetView();
		this.setAddMode(2);
		this.getTargetAddValue().setRelId(themeId);
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetInsert() {
		PMSLog.start();
		themeService.insertTarget(targetAddValue);
		this.prepareThemeView();
		this.prepareTargetView();		
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetDelete() {
		PMSLog.start();
		this.themeService.deleteTarget(targetId, SessionUtil.getUserId());
		this.prepareThemeView();
		this.prepareTargetView();		
		//this.prepareProgressView();
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetProgressList() {
		PMSLog.start();
		this.prepareThemeView();
		this.prepareTargetView();		
		this.prepareProgressView();
		this.addProgressValue=new ProgressValue();
		addProgressValue.setPgsUserId(SessionUtil.getUserId());
		addProgressValue.setPgsTs(DateUtil.getCurrentDate());
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetProgressInsert() {
		PMSLog.start();
		this.addProgressValue.setPgsTargetId(this.targetId);
		themeService.insertProgress(this.addProgressValue);
		this.prepareThemeView();
		this.prepareTargetView();		
		//this.prepareProgressView();
		PMSLog.end();
		return SUCCESS;
	}
	public String doReviewList() {
		PMSLog.start();
		this.prepareThemeView();
		this.prepareTargetView();		
		this.prepareReviewView();
		PMSLog.end();
		return SUCCESS;
	}
	public String doTargetReviewAdd() {
		PMSLog.start();
		
		this.prepareThemeView();
		this.prepareTargetView();
		this.prepareReviewView();
		this.setAddMode(3);
		this.addReviewValue.setReviewCreateDate(DateUtil.getCurrentDate());
		this.addReviewValue.setReviewExpectDate(DateUtil.getCurrentDate());
		this.addReviewValue.setReviewUserId(SessionUtil.getUserId());
		System.out.println(this.addReviewValue.getReviewUserId());
		this.addReviewValue.setReviewAdmitUserId(-1);
		this.addReviewValue.setReviewCycle(1);
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetReviewInsert() {
		PMSLog.start();
		addReviewValue.setReviewThemeId(themeId);
		addReviewValue.setReviewTargetId(targetId);
		addReviewValue.setReviewUserId(SessionUtil.getUserId());
		this.themeService.insertReview(addReviewValue);
		
		this.prepareThemeView();
		this.prepareTargetView();
		this.prepareReviewView();
		
		
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetReviewEdit1() {
		PMSLog.start();
		this.prepareThemeView();
		this.prepareTargetView();
		this.prepareReviewView();
		for(TargetValue tv : this.targetList) {
			if(tv.getTargetId()==this.targetId) {
				for(ReviewValue rv : tv.getReviewList()) {
					if(rv.getReviewId()==this.reviewId) {
						rv.setMode(1);
					}
				}
			}
		}
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetReviewEdit2() {
		PMSLog.start();
		this.prepareThemeView();
		this.prepareTargetView();
		this.prepareReviewView();
		for(TargetValue tv : this.targetList) {
			if(tv.getTargetId()==this.targetId) {
				for(ReviewValue rv : tv.getReviewList()) {
					if(rv.getReviewId()==this.reviewId) {
						rv.setReviewSolveUserId(SessionUtil.getUserId());
						rv.setReviewSolveDate(DateUtil.getCurrentDate());
						rv.setMode(2);
					}
				}
			}
		}
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetReviewUpdate() {
		PMSLog.start();
		for(TargetValue tv : this.targetList) {
			if(tv.getTargetId()==this.targetId) {
				for(ReviewValue rv : tv.getReviewList()) {
					if(rv.getReviewId()==this.reviewId) {
						System.out.println("★" + rv.getReviewThemeId() + ":" + rv.getReviewTargetId()+":" + rv.getReviewId() );
						rv.setReviewTargetId(targetId);
						rv.setReviewThemeId(themeId);
						this.themeService.updateReview(rv);
					}
				}
			}
		}
		this.prepareThemeView();
		this.prepareTargetView();
		this.prepareReviewView();
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetReviewDelete() {
		PMSLog.start();
		this.themeService.deleteReview(themeId, targetId, reviewId);
		this.prepareThemeView();
		this.prepareTargetView();		
		this.prepareReviewView();
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetReviewAdmit() {
		PMSLog.start();
		this.themeService.admitReview(SessionUtil.getUserId(), themeId, targetId, reviewId);
		this.prepareThemeView();
		this.prepareTargetView();		
		this.prepareReviewView();
		PMSLog.end();
		return SUCCESS;
	}
	
	public String doTargetReviewExport() {
		PMSLog.start();
		this.prepareThemeView();
		this.prepareTargetView();
		this.prepareReviewView();
		String projectName="";
		for(ProjectValue mv : this.projectList) {
			if(mv.getProjectId()==this.projectId) {
				projectName=mv.getProjectName();
			}
		}
		
		String themeName="";
		for(ThemeValue tv : this.themeList) {
			if(tv.getProjectId()==this.projectId) {
				themeName=tv.getThemeName();
			}
		}
		this.themeService.reviewExport(themeId, this.userList, this.targetList, themeName, projectName );
		PMSLog.end();
		return SUCCESS;
	}
	
}
